package com.sys.orders.back.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.pojo.ABlindbox;
import com.sys.goods.pojo.ABlindboxEnum;
import com.sys.goods.service.IGoodsDbService;
import com.sys.orders.pojo.AOrdersDeliver;
import com.sys.orders.pojo.AOrdersDesc;
import com.sys.orders.service.IOrdersDbService;

@Controller
@RequestMapping(value = "/back/orders",produces = "text/html;charset=UTF-8")
public class ordersDeliverBackController extends BaseController
{
	@Autowired
	private IOrdersDbService ordersDbService;
	@Autowired
	private IGoodsDbService goodsDbService;

	/**
	 * 发货列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersDeliverList")
	public String ordersDeliverList(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("====ordersDeliverList======");
		/* 生成分页对象 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 生成查询条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		
		String orderDetailsId = request.getParameter("orderDetailsId");
		if(orderDetailsId == null) {
			orderDetailsId = "" ; 
		}
		condMap.put("orderDetailsId", orderDetailsId);

		ApiResponse<AOrdersDeliver> apiResponse = this.ordersDbService
				.findCondListOrdersDeliverService(pageInfoUtil, condMap);

		request.setAttribute("response", apiResponse.toJSON());
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		return "/back/order/ordersDeliverList";
	}

	/**
	 * 发货更新
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/ordersDeliverUpdate")
	public String goodsUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		String orderDetailsId = request.getParameter("orderDetailsId");
		String blindBoxId = request.getParameter("blindBoxId");
		
		
		condMap.put("deliveryDetailsId", id);
//		condMap.put("extend", "true");
		ApiResponse<AOrdersDeliver> apiResponse = this.ordersDbService.findOneOrdersDeliverService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		condMap.clear();
		condMap.put("orderDetailsId", orderDetailsId);
		ApiResponse<AOrdersDesc> descapiResponse = this.ordersDbService.findOneOrdersDescService(condMap);
		request.setAttribute("descapiResponse", descapiResponse.toJSON());
		
		condMap.clear();
		condMap.put("blindBoxId", blindBoxId);
		ApiResponse<ABlindbox> boxcapiResponse = this.goodsDbService.findOneABlindboxService(condMap);
		request.setAttribute("boxcapiResponse", boxcapiResponse.toJSON());



		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		request.setAttribute("operType", operType);
		if ("update".equalsIgnoreCase(operType))
		{
			
			condMap.clear();
			condMap.put("status", ABlindboxEnum.STATUS_ENABLE.getStatus());
			ApiResponse<ABlindbox> boxApiResponse = this.goodsDbService.findCondListABlindboxService(null, condMap);
			request.setAttribute("boxResponse", boxApiResponse.toJSON());
			 ApiResponse<AOrdersDesc> descApiResponse = this.ordersDbService.findCondListOrdersDescService(null, condMap);
			request.setAttribute("descApiResponse", descApiResponse.toJSON());
			
//			request.setAttribute("operType", operType);
			
			return "/back/order/ordersDeliverUpdate";
		}
		return "/back/order/ordersDeliverInfo";
	}
	
	/**
	 * 发货更新提交
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/ordersDeliverUpdateSubmit")
	@ResponseBody
	public String goodsUpdateSubmit(HttpServletRequest request,Model model, HttpServletResponse response)
	{
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("deliveryDetailsId", id);
		ApiResponse<AOrdersDeliver> apiResponse = this.ordersDbService.findOneOrdersDeliverService(condMap);
		request.setAttribute("response", apiResponse.toJSON());

		AOrdersDeliver deliver = apiResponse.getDataOneJava();
		
		boolean flag = false;
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			/* 需要返回json */
			
			String orderDetailsId = request.getParameter("orderDetailsId");
			String blindBoxId = request.getParameter("blindBoxId");
			String purchaseQuantity = request.getParameter("purchaseQuantity");
			String status = request.getParameter("status");

			/* 赋值 */
			
			deliver.setBlindBoxId(Integer.valueOf(blindBoxId));
			deliver.setBlindBoxId(Integer.valueOf(blindBoxId));
			deliver.setPurchaseQuantity(Integer.valueOf(purchaseQuantity));
			
			deliver.setStatus(Byte.valueOf(status));
			

			flag = true;
		}
		

		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		if (flag)
		{
			apiResponseDb = this.ordersDbService.updateOneOrdersDeliverService(deliver);
		}

		/* 成功,提示 */
		JSONObject resultJSON = apiResponseDb.toJSON();
		return resultJSON.toJSONString();
	}
	
	
}
